import { getToken } from "@/utils/request";
import { uploadImage } from "@/api/product";
import { Session } from "@/utils/storage";
import bus from "@/utils/mitt";
// import { Session } from "@/utils/storage";

export const goArtical = (key) => {
  window.open(`/#/artical/content/${encodeURIComponent(key)}`, "_blank");
};

export const goGoods = (offerId) => {
  const token = getToken();
  if (token) window.open(`/#/goods/${offerId}`, "_blank");
  else {
    bus.emit("showLoginDialog");
  }
};

export const ORDER_STATUS = [
  "待支付", //0
  "待发货", //1
  "已发货", //2
  "已完成", //3
  "退款中", //4
  "已退款", //5
  "已过期", //6
  "已取消", //7
  "待审核", //8
  "待支付运费", //9
  "运费已付款", //10
];

/**
 * 根据订单状态判断第一阶段是否待支付
 * @param {*} status
 * @returns
 */
export const firstStepWaitPay = (status) => {
  return status == 0;
};
/**
 * 根据订单状态判断第一阶段是否已支付
 * @param {*} status
 * @returns
 */
export const firstStepPaied = (status) => {
  return [1, 2, 3, 4, 5, 8, 9, 10].includes(status);
};

/**
 * 根据订单状态判断第二阶段是否待支付
 * @param {*} status
 * @returns
 */
export const secondStepWaitPay = (status) => {
  return status == 9;
};
/**
 * 根据订单状态判断第二阶段是否已支付
 * @param {*} status
 * @returns
 */
export const secondStepPaied = (status) => {
  return [3, 10].includes(status);
};
/**
 * 将图片URL转为Base64
 * @param {*} url
 * @returns
 */
export const imageUrlToBase64 = async (url) => {
  return await compressImg(url);
};

export const imageToBase64 = async (file) => {
  return new Promise(function (resolve, reject) {
    let read = new FileReader();
    read.onload = async (e) => {
      let base64 = await compressImg(e.target.result);
      resolve(base64);
    };
    read.readAsDataURL(file);
  });
};

export const compressImg = (url) => {
  // var fileSize = parseFloat(parseInt(file["size"]) / 1024 / 1024).toFixed(2);
  // var read = new FileReader();
  // read.readAsDataURL(file);
  return new Promise(function (resolve, reject) {
    //   read.onload = function (e) {
    var img = new Image();
    img.setAttribute("crossOrigin", "anonymous");
    img.src = url;
    img.onload = function () {
      // 默认按比例压缩
      // var w = 300;
      // var rate = this.width / 300;
      // var h = this.height / rate;
      var w = this.width;
      // console.log("%c Line:112 🍞 this.width", "color:#93c0a4",url, this.width);
      var h = this.height;
      // 生成canvas
      var canvas = document.createElement("canvas");
      var ctx = canvas.getContext("2d");
      var base64;
      // 创建属性节点
      canvas.setAttribute("width", w);
      canvas.setAttribute("height", h);
      ctx.drawImage(this, 0, 0, w, h);
      base64 = canvas.toDataURL("image/jpeg", 0.2);
      resolve(base64);
    };
    //   };
  });
};

export const blobToFile = (theBlob, fileName) => {
  theBlob.lastModifiedDate = new Date(); // 文件最后的修改日期
  theBlob.name = fileName; // 文件名
  return new File([theBlob], fileName, {
    type: theBlob.type,
    lastModified: Date.now(),
  });
};

export const dataURLtoBlob = (dataurl) => {
  var arr = dataurl.split(","),
    mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], { type: mime });
};

export const imageUrlSearch = async (url) => {
  let imageUrl = await imageUrlToBase64(url);
  const { data } = await uploadImage({
    imageBase64: imageUrl,
  });
  // let imageList = Session.get("imageList") || [];
  let imageList = []; //只保留一张图片
  let item = {
    id: data.data,
    url: imageUrl,
  };
  imageList.push(item);
  Session.set("imageList", imageList);
  window.open("/#/imageSearch", "_blank");
};
